#include "CCThread.h"
#include <stdio.h>

int detach_thread_create(pthread_t * thread, void *start_routine, void* arg)
{
    pthread_attr_t attr;
    pthread_t thread_t;
    int ret;

    if (thread == NULL) {
        thread = &thread_t;
    }

    // 初始化线程的属性
    if (pthread_attr_init(&attr)) {
        printf("pthread_attr_init failed\n");
        return -1;
    }

    // 设置线程detach属性
    if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
        printf("pthread_attr_setdetachstate failed\n");
        pthread_attr_destroy(&attr);
    }

    ret = pthread_create(thread, &attr, (void *(*)(void *))start_routine, arg);
    if (ret < 0) {
        printf("pthread_create failed\n");
        pthread_attr_destroy(&attr);
    }

    ret = pthread_detach(thread_t);

    return ret;

}

